#include "systempreferencesdialog.h"
#include "ui_systempreferencesdialog.h"

#include "FramelessHelper/FramelessHelper.h"
#include <QPainter>
#include <QStyleOption>


SystemPreferencesDialog::SystemPreferencesDialog(QWidget *parent) :
    QDialog(parent, Qt::FramelessWindowHint),
    ui(new Ui::SystemPreferencesDialog)
{
    ui->setupUi(this);

    auto helper = new FramelessHelper(this);
    helper->setDraggableMargins(3, 3, 3, 3);
    helper->setMaximizedMargins(3, 3, 3, 3);
    helper->setTitleBarHeight(32);

    helper->addExcludeItem(ui->closeButton);
    connect(ui->closeButton, &QPushButton::clicked,
            helper, &FramelessHelper::triggerCloseButtonAction);

    setAttribute(Qt::WA_StyledBackground, true);

    // 初始化按钮图标
    ui->toolButton_general->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_general_checked.png"));
    ui->toolButton_autofocus->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_autofocus.png"));
    ui->toolButton_metrology->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_metrology.png"));
    ui->toolButton_sdk->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_sdk.png"));
    ui->toolButton_liveImage->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_liveImage.png"));
    ui->toolButton_sampleId->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_sampleId.png"));
    ui->toolButton_positions->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_positions.png"));
    ui->toolButton_travelLimits->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_travelLimits.png"));
    ui->toolButton_analysis->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_analysis.png"));
    ui->toolButton_users->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_users.png"));
    ui->toolButton_backup->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_backup.png"));
    ui->toolButton_recipes->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_recipes.png"));
    ui->toolButton_maintenance->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_maintenance.png"));
}

SystemPreferencesDialog::~SystemPreferencesDialog()
{
    delete ui;
}

void SystemPreferencesDialog::uncheckOtherButtons(QToolButton *checkedButton)
{
    QList<QToolButton*> allButtons = {
        ui->toolButton_general, ui->toolButton_autofocus, ui->toolButton_metrology,
        ui->toolButton_sdk, ui->toolButton_liveImage, ui->toolButton_sampleId,
        ui->toolButton_positions, ui->toolButton_travelLimits, ui->toolButton_analysis,
        ui->toolButton_users, ui->toolButton_backup, ui->toolButton_recipes,
        ui->toolButton_maintenance
    };
    for (QToolButton *button : allButtons) {
        if (button != checkedButton) {
            button->setChecked(false);
            updateButtonIcon(button, false);
        }
        else {
            button->setChecked(true);
            updateButtonIcon(button, true);
        }
    }
}

void SystemPreferencesDialog::updateButtonIcon(QToolButton *button, bool checked)
{
    if (button == ui->toolButton_general) {
        if (checked) {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_general_checked.png"));
        } else {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_general.png"));
        }
    } else if (button == ui->toolButton_autofocus) {
        if (checked) {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_autofocus_checked.png"));
        } else {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_autofocus.png"));
        }
    } else if (button == ui->toolButton_metrology) {
        if (checked) {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_metrology_checked.png"));
        } else {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_metrology.png"));
        }
    } else if (button == ui->toolButton_sdk) {
        if (checked) {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_sdk_checked.png"));
        } else {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_sdk.png"));
        }
    } else if (button == ui->toolButton_liveImage) {
        if (checked) {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_liveImage_checked.png"));
        } else {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_liveImage.png"));
        }
    } else if (button == ui->toolButton_sampleId) {
        if (checked) {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_sampleId_checked.png"));
        } else {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_sampleId.png"));
        }
    } else if (button == ui->toolButton_positions) {
        if (checked) {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_positions_checked.png"));
        } else {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_positions.png"));
        }
    } else if (button == ui->toolButton_travelLimits) {
        if (checked) {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_travelLimits_checked.png"));
        } else {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_travelLimits.png"));
        }
    } else if (button == ui->toolButton_analysis) {
        if (checked) {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_analysis_checked.png"));
        } else {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_analysis.png"));
        }
    } else if (button == ui->toolButton_users) {
        if (checked) {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_users_checked.png"));
        } else {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_users.png"));
        }
    } else if (button == ui->toolButton_backup) {
        if (checked) {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_backup_checked.png"));
        } else {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_backup.png"));
        }
    } else if (button == ui->toolButton_recipes) {
        if (checked) {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_recipes_checked.png"));
        } else {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_recipes.png"));
        }
    } else if (button == ui->toolButton_maintenance) {
        if (checked) {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_maintenance_checked.png"));
        } else {
            button->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_maintenance.png"));
        }
    }
    

}

void SystemPreferencesDialog::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,this);
    QDialog::paintEvent(event);
}

void SystemPreferencesDialog::on_toolButton_general_clicked(bool checked)
{
    if(checked)
    {
        uncheckOtherButtons(ui->toolButton_general);
        ui->stackedWidget->setCurrentWidget(ui->page_general);
    }
}

void SystemPreferencesDialog::on_toolButton_autofocus_clicked(bool checked)
{
    if(checked)
    {
        uncheckOtherButtons(ui->toolButton_autofocus);
        ui->stackedWidget->setCurrentWidget(ui->page_autofocus);
    }
}

void SystemPreferencesDialog::on_toolButton_metrology_clicked(bool checked)
{
    if(checked)
    {
        uncheckOtherButtons(ui->toolButton_metrology);
        ui->stackedWidget->setCurrentWidget(ui->page_metrology);
    }
}

void SystemPreferencesDialog::on_toolButton_sdk_clicked(bool checked)
{
    if(checked)
    {
        uncheckOtherButtons(ui->toolButton_sdk);
        ui->stackedWidget->setCurrentWidget(ui->page_sdk);
    }
}

void SystemPreferencesDialog::on_toolButton_liveImage_clicked(bool checked)
{
    if(checked)
    {
        uncheckOtherButtons(ui->toolButton_liveImage);
        ui->stackedWidget->setCurrentWidget(ui->page_liveImage);
    }
}

void SystemPreferencesDialog::on_toolButton_sampleId_clicked(bool checked)
{
    if(checked)
    {
        uncheckOtherButtons(ui->toolButton_sampleId);
        ui->stackedWidget->setCurrentWidget(ui->page_sampleId);
    }
}

void SystemPreferencesDialog::on_toolButton_positions_clicked(bool checked)
{
    if(checked)
    {
        uncheckOtherButtons(ui->toolButton_positions);
        ui->stackedWidget->setCurrentWidget(ui->page_positions);
    }
}

void SystemPreferencesDialog::on_toolButton_travelLimits_clicked(bool checked)
{
    if(checked)
    {
        uncheckOtherButtons(ui->toolButton_travelLimits);
        ui->stackedWidget->setCurrentWidget(ui->page_travelLimits);
    }
}

void SystemPreferencesDialog::on_toolButton_analysis_clicked(bool checked)
{
    if(checked)
    {
        uncheckOtherButtons(ui->toolButton_analysis);
        ui->toolButton_analysis->setIcon(QIcon(":/res/SystemPreferencesDialog/toolButton_analysis_checked.png"));
        ui->stackedWidget->setCurrentWidget(ui->page_analysis);
    }
}

void SystemPreferencesDialog::on_toolButton_users_clicked(bool checked)
{
    if(checked)
    {
        uncheckOtherButtons(ui->toolButton_users);
        ui->stackedWidget->setCurrentWidget(ui->page_users);
    }
}

void SystemPreferencesDialog::on_toolButton_backup_clicked(bool checked)
{
    if(checked)
    {
        uncheckOtherButtons(ui->toolButton_backup);
        ui->stackedWidget->setCurrentWidget(ui->page_backup);
    }
}

void SystemPreferencesDialog::on_toolButton_recipes_clicked(bool checked)
{
    if(checked)
    {
        uncheckOtherButtons(ui->toolButton_recipes);
        ui->stackedWidget->setCurrentWidget(ui->page_recipes);
    }
}

void SystemPreferencesDialog::on_toolButton_maintenance_clicked(bool checked)
{
    if(checked)
    {
        uncheckOtherButtons(ui->toolButton_maintenance);
        ui->stackedWidget->setCurrentWidget(ui->page_maintenance);
    }
}
